﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BuildPlannerLib.PlayerActions;
using BuildPlannerLib.SimEvents;
using BuildPlannerLib.Terran.Buildings;
using BuildPlannerLib.Terran.Enums;
using BuildPlannerLib.Buildings;

namespace BuildPlannerLib.Terran.PlayerActions
{
    // Derived class from Action for removing an addon from a Barracks/Factory/Starport
    public class RemoveAddon : PlayerAction
    {
        // Field for the building that will produce the addon
        public IndividualAddonBuilding Building;

        // Fields for the type of addon and type of building
        public AddonType AddonType;
        public string BuildingType;

        // Constructor with a parameter for the build, building type and addon type
        public RemoveAddon(Build build, string buildingType, AddonType addonType)
        {
            Build = build;
            BuildingType = buildingType;
            AddonType = addonType;
            // Lift-off/Land take 2 seconds each and a building moves 1 hex per second, making 7 seconds the minimum time for an add-on removal.
            TimeDuration = 700;
            Description = "Remove " + AddonType + " from " + BuildingType;
            LongDescription = "Removing an existing " + AddonType + " from a " + BuildingType + ".";
        }

        // Evaluates whether the player is Terran and whether there is a building of the specified type with the specified addon
        public override bool RequirementsFilled()
        {
            Error = null;
            AddonBuilding building = Build.Player.GetBuilding(BuildingType) as AddonBuilding;

            if (building == null)
            {
                Error = "This building can only be built by Terran players.";
                return false;
            }

            if (AddonType == AddonType.None)
            {
                Error = "Invalid addon type.";
                return false;
            }

            bool availableBuilding = building.IndividualBuildings.Exists(delegate(IndividualBuilding i)
            {
                IndividualAddonBuilding a = i as IndividualAddonBuilding;
                return a.AddonType == AddonType;
            });
            if (!availableBuilding)
            {
                Error = "There are none of the specified building available.";
                return false;
            }

            return base.RequirementsFilled();
        }

        // Evaluates if there is a building of the specified type with the specified addon idle
        public override bool StartCheck()
        {
            AddonBuilding building = Build.Player.GetBuilding(BuildingType) as AddonBuilding;

            bool buildingAvailable = building.IndividualBuildings.Exists(delegate(IndividualBuilding i)
            {
                IndividualAddonBuilding a = i as IndividualAddonBuilding;
                return a.AddonType == AddonType && !i.InUse && !a.AddonInUse;
            });

            if (!buildingAvailable)
                return false;

            return base.StartCheck();
        }

        // Adds an idle addon, removes the addon from the existing building, puts the building in-use, and adds the event for finishing the action
        public override void StartAction()
        {
            AddonBuilding building = Build.Player.GetBuilding(BuildingType) as AddonBuilding;
            TerranPlayer terranPlayer = Build.Player as TerranPlayer;

            base.StartAction();

            Building = building.IndividualBuildings.Find(delegate(IndividualBuilding i)
            {
                IndividualAddonBuilding a = i as IndividualAddonBuilding;
                return a.AddonType == AddonType && !i.InUse && !a.AddonInUse;
            }) as IndividualAddonBuilding;

            switch (AddonType)
            {
                case AddonType.Reactor:
                    terranPlayer.IdleReactor++;
                    break;
                case AddonType.TechLab:
                    terranPlayer.IdleTechLab++;
                    break;
            }

            Building.AddonType = AddonType.None;
            Building.StartProduction();
        }

        // Sets the building to not in-use
        public override void FinishAction()
        {
            base.FinishAction();

            Building.FinishProduction();
        }
    }
}
